package com.sims.action;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;
import com.sims.eo.StnUser;
import com.sims.service.IUserService;
import com.sims.service.impl.UserServiceImp;

public class LoginAction extends ActionSupport {
	private String userAccount;
	private String userPassword;
	private String userName;
	private String cellphone;

	public String getUserAccount() {
		return userAccount;
	}

	public void setUserAccount(String userAccount) {
		this.userAccount = userAccount;
	}

	public String getUserPassword() {
		return userPassword;
	}

	public void setUserPassword(String userPassword) {
		this.userPassword = userPassword;
	}

	public String getUserName() {
		return this.userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getCellphone() {
		return this.cellphone;
	}

	public void setCellphone(String cellphone) {
		this.cellphone = cellphone;
	}

	// 登录Action执行方法
	public String login() throws Exception {
		System.out.println("----login---");
		boolean validate = false;
		IUserService userVice = new UserServiceImp();
		validate = userVice.validateLogin(userAccount, userPassword);
		if (validate) {
			// 设置当前登录的用户到Session中
			ActionContext.getContext().getSession().put("currentUserAccount", userAccount);
			ServletActionContext.getRequest().getSession().putValue("currentUserAccount", userAccount);
			
			return "success";
		} else {
			return "error";
		}
	}

	// 退出Action执行方法
	public String logout() throws Exception {
		System.out.println("----logout---");
		// 清空session对象中当前账号信息
		if (ActionContext.getContext().getSession().containsKey("currentUserAccount")) {
			ActionContext.getContext().getSession().remove("currentUserAccount");
			
		}
		return this.INPUT;
	}

	// 登录Action校验方法
	public void validateLogin() {
		System.out.println("----validateLogin---");
		if (userAccount == null || userAccount.trim().length() <= 0) {
			this.addFieldError("userAccountInfo", "请输入账号 ！");
		}
		if (userPassword == null || userPassword.trim().length() <= 0) {
			this.addFieldError("userPasswordInfo", "请输入密码 ！");
		}
	}

	// 注册Action执行方法
	@InputConfig(resultName = "registe_input")
	public String registeAccount() throws Exception {
		System.out.println("----registeAccount---");
		StnUser user = new StnUser();
		user.setUserAccount(userAccount);
		user.setUserName(userName);
		user.setUserPassword(userPassword);
		user.setCellphone(cellphone);
		IUserService userVice = new UserServiceImp();
		Long id = userVice.createStnUser(user);
		if (id > 0) {
			// 注册成功
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("text/html;charset=UTF-8");
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			out.println("<script>alert('注册成功,请重新登录。');window.location.href='login.jsp';</script>");
			out.flush();
			out.close();
			return null;
		} else {
			// 注册失败
			this.addActionError(userAccount + ",账号注册失败！");
			return "registe";
		}
	}

	// 注册Action校验方法
	public void validateRegisteAccount() throws Exception {
		System.out.println("----validateRegisteAccount---");
		if (userAccount == null || userAccount.trim().length() <= 0) {
			this.addFieldError("userAccountInfo", "请输入账号 ！");
		}
		if (userPassword == null || userPassword.trim().length() <= 0) {
			this.addFieldError("userPasswordInfo", "请输入密码 ！");
		}
	}
}
